//
//  ESCommentVC.m
//  Eyeshot
//
//  Created by admin on 15/7/7.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESCommentVC.h"
#import "ESDuanziTableCell.h"
#import "ESTitleView.h"
#import "ESComment.h"
#import "ESCommentCell.h"

@interface ESCommentVC ()
@property (nonatomic, strong) NSMutableArray *models;
@end

@implementation ESCommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = ESColor(234, 234, 234);
    [self.tableView registerClass:[ESCommentCell class] forCellReuseIdentifier:@"CommentCell"];
    [self.tableView registerClass:[ESDuanziTableCell class] forCellReuseIdentifier:@"DuanziCell"];
    
    
    //添加中间的标题titleView
    ESTitleView *titleView = [[ESTitleView alloc] init];
    titleView.width = 20 * kHorizen;
    titleView.height = 7 * kVertical;
    titleView.title = @"评论";
    self.navigationItem.titleView = titleView;
    
    _models = [NSMutableArray array];
    
    [self requestData];
}

#pragma mark - 网络请求

- (void)requestData {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *reqStr = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *reqParas = @{@"a": @"dataList",
                               @"appname": @"baisishequ",
                               @"asid": @"5E6124E7-C271-4F3F-9FE6-33E54108A2F3",
                               @"c": @"comment",
                               @"client": @"iphone",
                               @"data_id": @"14753684",
                               @"device": @"iPhone4S",
                               @"hot": @"1",
                               @"jbk": @"0",
                               @"mac": @"",
                               @"market": @"",
                               @"openudid": @"915b9db78a08fe308fdeda8a949ba0f436c6be8f",
                               @"page": @"1",
                               @"per": @"50",
                               @"udid": @"",
                               @"uid": @"15150267",
                               @"ver": @"3.6"
                               };
    [mgr GET:reqStr parameters:reqParas success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        
        NSArray *responseArray = responseObject[@"data"];
        for (NSDictionary *dict in responseArray) {
            ESComment *model = [ESComment objectWithKeyValues:dict];
            [_models addObject:model];
        }
        
//        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ESLog(@"%@",error);
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
        return 1;
    }else {
        return 10;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return [ESDuanziTableCell heightForCellWithModel:_model];
    } else {
        return 60;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        ESDuanziTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DuanziCell"];
        cell.model = _model;
        return cell;
    } else {
        ESCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
        return cell;
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        
        return @"最新评论";
    }
    return nil;
}

@end
